package com.example.springboot.controller;

import com.example.springboot.common.AuthAccess;
import com.example.springboot.common.Page;
import com.example.springboot.common.Result;
import com.example.springboot.entity.User;
import com.example.springboot.entity.Wine;
import com.example.springboot.service.WineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController

@RequestMapping("/wine")
public class WineController {
    @Autowired
    WineService wineService;
    /*
   新增用户信息
    */
    @AuthAccess
    @PostMapping("/add")
    public Result add(@RequestBody Wine wine) {
        try {
            wineService.insertWine(wine);
        } catch (Exception e){
            if (e instanceof DuplicateKeyException){
                return Result.error("插入数据库错误");
            }else {
                return Result.error("系统错误");
            }
        }
        return Result.success();
    }
    /*
    修改用户信息
     */
    @PutMapping("/update")
    public Result update(@RequestBody Wine wine){
        wineService.updateWine(wine);
        return Result.success();
    }
    /*
    删除用户信息
     */
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id){
        wineService.deleteWine(id);
        return Result.success();
    }
    /*
    批量删除用户信息
     */
    @DeleteMapping("/delete/batch")
    public Result batchDelete(@RequestBody List<Integer> ids){
        wineService.batchDeleteWine(ids);
        return Result.success();
    }
    /*
    查询全部用户信息
    */
    @AuthAccess
    @GetMapping("/selectAll")
    public Result selectAll(){
        List<Wine> wineList = wineService.selectAll();
        return Result.success(wineList);
    }
    /*
    根据id查询
    */
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id){
        Wine wine = wineService.selectById(id);
        return Result.success(wine);
    }
    /*
    根据name查询
    */
    @GetMapping("/selectByName/{name}")
    public Result selectByName(@PathVariable String name){
        List<Wine> wineList = wineService.selectByName(name);
        return Result.success(wineList);
    }

}
